package com.cz.fra.android;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	private Button register_post;
	private Button cancel;
	private EditText register_password;
	private EditText register_name;
	private EditText register_email;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initView();
		FishReportApplication.getInstance().addActivities(this);
	}
	
	private void initView() {
		register_post = (Button) findViewById(R.id.register_post);
		cancel = (Button) findViewById(R.id.cancel);
		register_name = (EditText) findViewById(R.id.register_name);
		register_password = (EditText) findViewById(R.id.register_password);
		register_email = (EditText) findViewById(R.id.register_password);
		cancel.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				finish();
				return true;
			}
		});
    	register_post.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				new Thread(runnable).start();
				return true;
			}
		});
    }
	
	Runnable runnable = new Runnable(){

		@Override
		public void run() {
			HttpPost httpRequest = new HttpPost(Config.URL_REGISTER);
			List<NameValuePair>	nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("username", register_name.getText()+""));
			nameValuePairs.add(new BasicNameValuePair("password", register_password.getText()+""));
			nameValuePairs.add(new BasicNameValuePair("email", register_email.getText()+""));
			try {
				httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
				HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 
				if(httpResponse.getStatusLine().getStatusCode() == 200) {
					System.out.println("------------------------");
					Message msg = new Message();
					msg.what = Config.REGISTER_SUCCESS;
					mHandler.sendMessage(msg);
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}

	};
	
	private Handler mHandler = new Handler() {
    	public void handleMessage(Message msg) {
    		switch (msg.what) {
    		    case Config.REGISTER_SUCCESS:
    		    	showDialg();
    		    	break;
    		}
    	}
    };
    
    private void showDialg() {
    	new AlertDialog.Builder(this)
    	.setTitle(getResources().getString(R.string.register_success))
    	.setMessage(getResources().getString(R.string.success))
    	.setNegativeButton(getResources().getString(R.string.submit), new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		})
    	.show();
    }
    
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
            exitDialog();  
            return true;  
        }  
        return true;  
    }

	private void exitDialog() {
		AlertDialog.Builder builder = new Builder(this);  
        builder.setMessage("Confirm exit?");  
        builder.setTitle("Alert");  
        builder.setPositiveButton("YES",  
        new android.content.DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
                FishReportApplication.getInstance().exit();
            }  
        });  
        builder.setNegativeButton("NO",  
        new android.content.DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
            }  
        });  
        builder.create().show();  
	}
}
